package com.jiguangssp.addemo.activity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.jiguangssp.addemo.R;
import com.jiguangssp.addemo.constant.ADJgDemoConstant;
import com.jiguangssp.addemo.manager.ADJgInterstitialManager;

import cn.jiguang.jgssp.ad.ADJgInterstitialAd;
import cn.jiguang.jgssp.ad.data.ADJgInterstitialAdInfo;
import cn.jiguang.jgssp.ad.entity.ADJgExtraParams;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgInterstitialAdListener;
import cn.jiguang.jgssp.util.ADJgAdUtil;

/**
 * @author ciba
 * @description 插屏广告示例
 * @date 2020/3/27
 */
public class InterstitialAdAutoCloseActivity extends BaseAdActivity implements View.OnClickListener {
    private ADJgInterstitialAd interstitialAd;
    private ADJgInterstitialAdInfo interstitialAdInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interstitial_ad_auto_close);

        initListener();
        initAd();
    }

    private void initListener() {
        Button btnLoadAd = findViewById(R.id.btnLoadAd);
        Button btnShowAd = findViewById(R.id.btnShowAd);
        CheckBox cbAutoClose = findViewById(R.id.cbAutoClose);

        btnLoadAd.setText("获取插屏广告");
        btnShowAd.setText("展示插屏广告");

        btnLoadAd.setOnClickListener(this);
        btnShowAd.setOnClickListener(this);

        cbAutoClose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                ADJgDemoConstant.INTERSTITIAL_AD_AUTO_CLOSE = b;
            }
        });
    }

    private void initAd() {
        interstitialAd = new ADJgInterstitialAd(this);
        // 设置仅支持的广告平台，设置了这个值，获取广告时只会去获取该平台的广告，null或空字符串为不限制，默认为null，方便调试使用，上线时建议不设置。注：仅debug模式为true时生效。
        interstitialAd.setOnlySupportPlatform(ADJgDemoConstant.INTERSTITIAL_AD_ONLY_SUPPORT_PLATFORM);
        // 创建额外参数实例
        ADJgExtraParams extraParams = new ADJgExtraParams.Builder()
                // 设置视频类广告是否静音
                .setVideoWithMute(ADJgDemoConstant.INTERSTITIAL_AD_PLAY_WITH_MUTE)
                .build();
        interstitialAd.setLocalExtraParams(extraParams);
        // 设置插屏广告监听
        interstitialAd.setListener(new ADJgInterstitialAdListener() {
            @Override
            public void onAdReady(ADJgInterstitialAdInfo interstitialAdInfo) {
                Log.d(ADJgDemoConstant.TAG, "onAdReady...");
            }

            @Override
            public void onAdReceive(ADJgInterstitialAdInfo interstitialAdInfo) {
                // TODO 插屏广告对象一次成功拉取的广告数据只允许展示一次
                InterstitialAdAutoCloseActivity.this.interstitialAdInfo = interstitialAdInfo;
                Log.d(ADJgDemoConstant.TAG, "onAdReceive...");
            }

            @Override
            public void onAdExpose(ADJgInterstitialAdInfo interstitialAdInfo) {
                Log.d(ADJgDemoConstant.TAG, "onAdExpose...");

                if (ADJgDemoConstant.INTERSTITIAL_AD_AUTO_CLOSE) {
                    ADJgInterstitialManager.getInstance().addJumpView(interstitialAdInfo, InterstitialAdAutoCloseActivity.this);
                }
            }

            @Override
            public void onAdClick(ADJgInterstitialAdInfo interstitialAdInfo) {
                Log.d(ADJgDemoConstant.TAG, "onAdClick...");
            }

            @Override
            public void onAdClose(ADJgInterstitialAdInfo interstitialAdInfo) {
                Log.d(ADJgDemoConstant.TAG, "onAdClose...");
            }

            @Override
            public void onAdFailed(ADJgError adJgError) {
                if (adJgError != null) {
                    String failedJson = adJgError.toString();
                    Log.d(ADJgDemoConstant.TAG, "onAdFailed..." + failedJson);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnLoadAd:
                loadAd();
                break;
            case R.id.btnShowAd:
                ADJgAdUtil.showInterstitialAdConvenient(this, interstitialAdInfo);
                break;
            default:
                break;
        }
    }

    /**
     * 加载广告
     */
    private void loadAd() {
        // 插屏广告场景id（场景id非必选字段，如果需要可到开发者后台创建）
        interstitialAd.setSceneId(ADJgDemoConstant.INTERSTITIAL_AD_SCENE_ID);
        // 加载插屏广告，参数为广告位ID
        interstitialAd.loadAd(ADJgDemoConstant.INTERSTITIAL_AD_POS_ID);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ADJgDemoConstant.INTERSTITIAL_AD_AUTO_CLOSE = false;
    }
}
